package core.plugins;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import core.ModelObject;
import core.ProductManager;

/**
 * This is basically a manager and it
 * handles all of the plugin loading and
 * creation.  This also is responsible for 
 * creating the chain of responsiblity for
 * the plugins.
 * 
 * @author Spencer
 *
 */

public class PluginRegistry extends ModelObject 
{	
	/**
	 * 
	 */
	private static final long serialVersionUID = 8603613574753985794L;

	/** 
	 * This holds a pointer to the first plugin in the
	 * IProductIDPlugin chain of responsibility.
	 */
	private IProductIDPlugin idProductChain = null;
	
	/**
	 * This is the filename that holds all of the plugins
	 * to load.
	 */
	private final String ProductIDPluginFile = "ProductIDPlugins.pr";
	
	/** This variable holds the only instance of this class. */
	private static PluginRegistry instance = null;
	
	public static PluginRegistry getInstance()
	{
		if (instance == null)
		{
			instance = new PluginRegistry();
		}
		return instance;
	}
	
	/**
	 * Default constructor.
	 */
	private PluginRegistry()
	{
		loadProductIDPlugins();
	}
	
	/**
	 * This method will look through the plugin
	 * registry and install all the plugins
	 * with the IProductIDPlugin interface and
	 * link them into a chain.
	 * 
	 * @return True if the ProductID plugins were
	 * 		successfully loaded.
	 */
	public boolean loadProductIDPlugins()
	{
		//did this method succeed
		boolean success = true;
		//open the ProductIDPluginFile
		BufferedReader plugins = null;
		
		try 
		{
			plugins = new BufferedReader(new FileReader(ProductIDPluginFile));
			
			String currentLine;
			
			IProductIDPlugin currentPlugin = null;
			
			while( (currentLine = plugins.readLine()) != null)
			{
				//ignore blank lines and comment lines (lines that start with #)
				if (currentLine.length() == 0) continue;
				if (currentLine.charAt(0) == '#') continue;
				
				//install every plugin
				IProductIDPlugin newPlugin = PluginInstaller.installPlugin(currentLine); 
				if (newPlugin != null)
				{
					if (currentPlugin == null)
					{
						//this is the first plugin
						//set the first plugin in the chain
						idProductChain = newPlugin;
						//update the current plugin
						currentPlugin = newPlugin;
					}
					else
					{
						//This isn't the first one so link
						//this plugin onto the current one
						currentPlugin.setNextPlugin(newPlugin);
						//update the current plugin
						currentPlugin = newPlugin;
					}
				}
			}
		} 
		catch (IOException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
			success = false;
		}
		finally
		{
			try 
			{
				if (plugins != null) plugins.close();
			} 
			catch (IOException e) 
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
		return success;
	}
	
	/**
	 * Get the plugin that is used for Identifying
	 * the products, and return it to the caller.
	 * 
	 * @return Returns the first plugin in
	 * 		the productIDPlugin chain.
	 */
	public IProductIDPlugin getProductIDPlugin()
	{
		return idProductChain;
	}
}
